local TALENT_SELECTION = {}

function TALENT_SELECTION:Init()
	self.Icon = "devin/blackbetty/hudicons/inventoryslot.png"
	self.NumCol = 1
	self.NumRows = 1
	self.IconSize = 48
	self.CellHeight = 48
	self.CellWidth = 48
	self.HoveredCell = 0
	self.OldHoveredCell = 1
	self.Tier = 0
	self.CellInfo = {}
	self.CellInfo[1] = {}
	self.CellInfo[1].TierNumber = 0
	self.CellInfo[1].X_Offset = 0
	self.CellInfo[1].Y_Offset = 0
	self.CellInfo[1].Color = Color(255, 255, 255, 255)
	self.ClickedCell = true
	self.CellToDraw = 1
	self.SelectedTalent = 1
	self:CreateCells()
	self:RebuildTalents()
end

function TALENT_SELECTION:SetTier(tier)
	self.Tier = tier
end

function TALENT_SELECTION:SetMode(mode)
	self.Mode = mode
end

function TALENT_SELECTION:SetIconSize(size)
	self.IconSize = size
	self:CreateCells()
	self:RebuildTalents()
end

function TALENT_SELECTION:SetCellWidth(width)
	self.CellWidth = width
	self:CreateCells()
	self:RebuildTalents()
end

function TALENT_SELECTION:SetCellHeight(height)
	self.CellHeight = height
	self:CreateCells()
	self:RebuildTalents()
end

function TALENT_SELECTION:SetCols(cols)
	self.NumCol = cols
	self:CreateCells()
	self:RebuildTalents()
end

function TALENT_SELECTION:SetRows(rows, talents)
	self.NumRows = rows
	self:CreateCells()
	self:RebuildTalents(talents)
end


function TALENT_SELECTION:CreateCells()
	self.CellInfo = {}
	local CellNum = 1
	for i = 1, self.NumRows, 1 do
		for k = 1, self.NumCol, 1 do
			self.CellInfo[CellNum] = {}
			self.CellInfo[CellNum].TierNumber = 0
			self.CellInfo[CellNum].X_Offset = ((k * self.CellWidth)-self.CellWidth)
			self.CellInfo[CellNum].Y_Offset = ((i * self.CellHeight)-self.CellHeight)
			self.CellInfo[CellNum].Color = Color(255, 255, 255, 255)
			self.CellInfo[CellNum].DefaultColor = Color(255, 255, 255, 255)
			self.CellInfo[CellNum].Icon = "devin/blackbetty/hudicons/inventoryslot.png"
			CellNum = CellNum + 1
		end
	end
	PrintTable(self.CellInfo)
end

function TALENT_SELECTION:CalcCells(talents)
	local cell_inc = 0
	if(#talents>0)then
		for k, v in pairs(talents)do
			if(v)then
				print(v)
				cell_inc = cell_inc + 1
			end
		end
		self:SetRows(cell_inc, talents)
	end
end

function TALENT_SELECTION:RebuildTalents(talents)
	if(talents and #talents > 0)then
		for k, v in pairs(self.CellInfo)do
			if(talents[k] and talents[k] != 0)then 
				v.Icon = GetSpellByID(ClassData.LevelData[playerClass][self.Tier][2][1][k])[2]
				v.SpellName = GetSpellByID(ClassData.LevelData[playerClass][self.Tier][2][1][k])[1]
				v.SpellDesc = GetSpellByID(ClassData.LevelData[playerClass][self.Tier][2][1][k])[3]
				v.UpgradeType = ClassData.LevelData[playerClass][self.Tier][1]
				if(string.len(v.SpellDesc)>55)then
					local s = string.sub(v.SpellDesc, 0, 55)
					local lastspace =  string.find(s, " ", -6)
					local s1 = ""
					local s2 = ""
					if(lastspace)then
						s1 = string.sub(v.SpellDesc, 0, lastspace)
						s2 = string.sub(v.SpellDesc, lastspace+1)
					else
						s1 = string.sub(v.SpellDesc, 0, 55)
						s2 = string.sub(v.SpellDesc, 56)
					end
					v.SpellDesc = s1
					v.SpellDescOverflow = s2
					print(v.SpellDesc)
				end
				if(self.Mode == "selected")then
					if(selectedTalents[self.Tier] == k)then
						v.DefaultColor = Color(115, 255, 115, 255)
						v.Color = Color(115, 255, 115, 255)
					else
						v.DefaultColor = Color(115, 115, 115, 255)
						v.Color = Color(115, 115, 115, 255)
					end
				elseif(self.Mode == "unselected")then
					v.DefaultColor = Color(255, 255, 255, 255)
					v.Color = Color(255, 255, 255, 255)
				elseif(self.Mode == "unearned")then
					v.DefaultColor = Color(255, 50, 50, 255)
					v.Color = Color(255, 50, 50, 255)
				end
			end
		end
	end
end

function TALENT_SELECTION:Paint()
	for i = 1, #self.CellInfo, 1 do
		surface.SetMaterial(Material( "devin/blackbetty/tooltip/window.png" ) )
		surface.SetDrawColor(self.CellInfo[i].Color)
		surface.DrawTexturedRect(self.CellInfo[i].X_Offset, self.CellInfo[i].Y_Offset, self.CellWidth, self.CellHeight)
		
		local struc3 = {}
		struc3.pos = {}
		struc3.pos[1] = self.CellInfo[i].X_Offset+self.IconSize
		struc3.pos[2] = self.CellInfo[i].Y_Offset+(self.CellHeight*0.15)
		struc3.color = Color(255,255,0,255)
		struc3.text = ""..self.CellInfo[i].SpellName..""
		struc3.font = "HUDText_Quests"
		struc3.xalign = TEXT_ALIGN_LEFT
		struc3.yalign = TEXT_ALIGN_BOTTOM
		draw.Text( struc3 )
		
		local struc4 = {}
		struc4.pos = {}
		struc4.pos[1] = self.CellInfo[i].X_Offset+self.IconSize
		struc4.pos[2] = self.CellInfo[i].Y_Offset+(self.CellHeight*0.5)
		struc4.color = Color(255,255,0,255)
		struc4.text = self.CellInfo[i].SpellDesc
		struc4.font = "HUDText_XPBar"
		struc4.xalign = TEXT_ALIGN_LEFT
		struc4.yalign = TEXT_ALIGN_TOP
		draw.Text( struc4 )
		
		if(self.CellInfo[i].SpellDescOverflow)then
			local struc5 = {}
			struc5.pos = {}
			struc5.pos[1] = self.CellInfo[i].X_Offset+self.IconSize
			struc5.pos[2] = self.CellInfo[i].Y_Offset+(self.CellHeight*0.7)
			struc5.color = Color(255,255,0,255)
			struc5.text = self.CellInfo[i].SpellDescOverflow
			struc5.font = "HUDText_XPBar"
			struc5.xalign = TEXT_ALIGN_LEFT
			struc5.yalign = TEXT_ALIGN_TOP
			draw.Text( struc5 )
		end
		
		surface.SetMaterial(Material( self.CellInfo[i].Icon ) )
		surface.SetDrawColor(self.CellInfo[i].Color)
		surface.DrawTexturedRect(self.CellInfo[i].X_Offset, self.CellInfo[i].Y_Offset, self.IconSize, self.IconSize)
	end
end

function TALENT_SELECTION:Think()
	local xpos, ypos = self:LocalCursorPos()
	local xmult_ = math.Round((xpos/self.CellWidth)+0.5, 0)		
	local ymult_ = math.Round((ypos/self.CellHeight)+0.5, 0)
	local xmult = math.Clamp(xmult_, 0, self.NumCol)
	local ymult = math.Clamp(ymult_, 0, self.NumRows)
	if((xpos >= 0 and ypos >= 0) and ((xpos <= self.CellWidth * self.NumCol) and (ypos <= self.CellHeight * self.NumRows)))then
		self.HoveredCell = math.Round((xmult + ((ymult*self.NumCol)-self.NumCol)), 0)
		self.CellInfo[self.HoveredCell].Color = Color(155, 255, 155, 255)
		if(input.IsMouseDown(MOUSE_LEFT))then
			self.SelectedTalent = self.HoveredCell
			if(!self.ClickedCell and (self.Mode != "selected" and self.Mode != "unearned"))then
				self.ClickedCell = true
				surface.PlaySound("buttons/lightswitch2.wav", 60, 10000)
				self.Mode = "selected"
				selectedTalents[self.Tier] = self.HoveredCell
				net.Start("SendTalentSelection")
				net.WriteTable(selectedTalents)
				net.SendToServer()
			end
		else
			self.ClickedCell = false
		end
	end
	if(self.HoveredCell != self.OldHoveredCell and self.HoveredCell > 0 or !(((xpos <= self.CellWidth * self.NumCol) and (ypos <= self.CellHeight * self.NumRows))))then
		self.CellInfo[self.OldHoveredCell].Color = self.CellInfo[self.OldHoveredCell].DefaultColor
		if(self.HoveredCell > 0)then
			self.OldHoveredCell = self.HoveredCell
		end
	end
end

vgui.Register( "TalentSelection", TALENT_SELECTION, "DImage")